<template>
  <!--备件采购信息	-->
  <div>
    <FormPanel ref="FormPanel" :list="initForm" :cols="3" :formType="formType" :labelWidth="180" :isSubmitBtn="false" @formChange="formChangeHandle" class="mtop" />
  </div>
</template>

<script>
  import { dictionary } from '@/utils/dictMixin';
  import * as types from '@/api/cms-api/origin/originInfo/types';
  import {phoneAndSolidify,email} from '@/utils/validate.js';
  import vm from './bridge';

  export default {
    mixins: [dictionary],
    model: {
      prop: 'pdata',
      event: 'click'
    },
    props: {
      pdata: {
        type: Object,
        default: () => ({})
      },
      formType: {
        type: String,
        required: true
      },
      formOptions: {
        type: Object,
        required: true
      }
    },
    data() {
      return {
        initForm: this.initFormFun()
      };
    },
    mounted() {
      //注册经销商类型变化时触发的事件
      //在审核时，给此页面的类型为只读，但是基本信息（infoBase可以编辑，会显示会触发事件，事件会触发禁用取消，覆盖整个表单的类型（表单类型为show，但是依旧可以输入，（部分）））
      if (this.formType != 'show') {
        vm.$on('changeDealerType', this.changeDealerType);
      }
    },
    methods: {
      //初始化数据
      init(data) {
        this.initForm.forEach(x => {
          if (this.pdata[x.fieldName]) {
            x.initialValue = this.pdata[x.fieldName];
          }
        });
      },
      //初始化数据（带触发事件，为经销商提报添加的）
      initPre(data) {
        if (this.formType != 'show') {
          if (!this.pdata.companyName) {
            this.pdata.companyName = this.pdata.dealerName;
          }
          // vm.$emit('changeDealerType', this.pdata);
          vm.$emit('changeDealerType', this.pdata.dealerType);
          // this.supplementChangeDealerType(this.pdata.dealerType);
        }
        this.initForm.forEach(x => {
          if (this.pdata[x.fieldName]) {
            x.initialValue = this.pdata[x.fieldName];
          }
        });
      },
      //表单验证通过后
      formChangeHandle(val) {
        this.pdata = Object.assign(this.pdata, val);
        //传递数据到父组件里
        this.$emit('click', this.pdata);
        //触发外部的事件，以标志着本表单已经准备完毕
        // this.$emit('formchange', this.pdata);
      },
      //开放的提交方法
      async saveDialog() {
        let flag = true;
        let err = this.$refs.FormPanel.SUBMIT_FORM();
        await this.$refs.FormPanel.$refs.form.validate().then(res => {
          console.log("res1:", res);
        }).catch(err => {
          console.log("err1:", err);
          flag = false;
        });
        console.log("flag:", flag);
        return flag;
        // this.$notify.warning({title: '提示信息',message:'只能选择一条记录'});
      },
      //经销商类型变更时
      changeDealerType(value) {
        //判断是否是dle页面，是则使用dlr专用验证
        let suffix = this.formOptions.formType === types.DEALER_INFO_FORM_STATUS_SUBMISSION_DLR ? 'DLR' : '';
        //根据类型处理验证
        if (types.DEALER_INFO_DEALER_TYPE_4S === value) {
          //4S
          vm.changeForm(this.initForm, '4S' + suffix, false);
          vm.clearValidate(this.$refs.FormPanel);
        } else if (types.DEALER_INFO_DEALER_TYPE_SINGLE_AFTER_SALE === value) {
          //单一售后
          vm.changeForm(this.initForm, 'SH' + suffix, false);
          vm.clearValidate(this.$refs.FormPanel);
        } else if (types.DEALER_INFO_DEALER_TYPE_SINGLE_SALE === value) {
          //单一销售
          vm.changeForm(this.initForm, 'XS' + suffix, false);
          vm.clearValidate(this.$refs.FormPanel);
        } else if (types.DEALER_INFO_DEALER_TYPE_DIRECT_SHOP === value) {
          //SPACE
          vm.changeForm(this.initForm, 'SPACE' + suffix, false);
          vm.clearValidate(this.$refs.FormPanel);
        } else if (types.DEALER_INFO_DEALER_TYPE_NO_DIRECT_SHOP === value) {
          //RACE
          vm.changeForm(this.initForm, 'RACE' + suffix, false);
          vm.clearValidate(this.$refs.FormPanel);
        } else if (types.DEALER_INFO_DEALER_TYPE_BIG_CUSTOMER === value) {
          //大客户
          vm.changeForm(this.initForm, 'DKH' + suffix, false);
          vm.clearValidate(this.$refs.FormPanel);
        }
        //this.supplementChangeDealerType(value);
      },
      //补充验证
      supplementChangeDealerType(value) {
        if (this.formOptions.formType === types.DEALER_INFO_FORM_STATUS_SUBMISSION_DLR) {
          //单一售后、RACE、SPACE店首笔提车款附件不必填
          if (types.DEALER_INFO_DEALER_TYPE_SINGLE_AFTER_SALE === value || types.DEALER_INFO_DEALER_TYPE_DIRECT_SHOP === value || types.DEALER_INFO_DEALER_TYPE_NO_DIRECT_SHOP === value) {
            //首笔提车款附件
            this.initForm.find(x => x.fieldName === 'firstPaymentEnclosureList').rules = [];
            //4S店和单一销售必填
          } else if (types.DEALER_INFO_DEALER_TYPE_4S === value || types.DEALER_INFO_DEALER_TYPE_SINGLE_SALE === value) {
            this.initForm.find(x => x.fieldName === 'firstPaymentEnclosureList').rules = [{ required: true, message: '必填', trigger: 'blur' }];
          } else {
            this.initForm.find(x => x.fieldName === 'firstPaymentEnclosureList').rules = [];
          }
        }
      },
      //表单数据
      initFormFun() {
        let bt = { required: true, message: '必填', trigger: ['change', 'blur'] };
        let btl = [bt];
        let sj = { pattern: /^[1][3,4,5,7,8][0-9]{9}$/, message: '请填写正确的手机号', trigger: ['change', 'blur'] };
        let email = { type: 'email', message: '请输入正确的邮箱', trigger: 'blur' };
        let jy = { disabled: true };
        let yc = { hidden: true };
        return [
          {
            type: 'SELECT',
            label: '备件仓库',
            fieldName: 'partStock',
            placeholder: '请选择',
            initialValue: 16241001,
            itemList: this.createDictList('1624'),
            rules: btl,
          },
          {
            type: 'INPUT',
            label: '收货人',
            fieldName: 'stockMan',
            rows: 1,
            initialValue: '',
            placeholder: '请输入',
            maxlength: 30,
            rules: btl,
          },
          {
            type: 'INPUT',
            label: '收货人电话',
            fieldName: 'stockManTel',
            rows: 1,
            initialValue: '',
            placeholder: '请输入',
            maxlength: 50,
            rules: [
              bt,
              { validator: phoneAndSolidify, trigger: 'blur'}
            ]
          },
          {
            type: 'SELECT',
            label: '批售授权服务商',
            fieldName: 'isAuthService',
            placeholder: '请选择',
            initialValue: '',
            rules: btl,
            itemList: this.createDictList('1004'),
            hidden: true,
          },
          {
            type: 'INPUT',
            label: '仓库收货地址',
            fieldName: 'stockAddress',
            rows: 1,
            selfCols: 2,
            initialValue: '',
            placeholder: '请输入',
            maxlength: 126,
            rules: btl,
          },

        ];
      }
    }
  };
</script>
